import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:industry_net/base/common/util.dart';
import 'package:industry_net/base/component/component.dart';
import 'package:industry_net/base/component/list/sliver_table_view.dart';
import 'controller.dart';
import 'views/header.dart';

class ContentPage extends StatefulWidget {
  const ContentPage({Key? key}) : super(key: key);

  @override
  _ContentPageState createState() => _ContentPageState();
}

class _ContentPageState extends State<ContentPage>
    with SingleTickerProviderStateMixin {
  final controller = Get.find<AppMoreController>();
  @override
  void initState() {
    super.initState();
    controller.state.tabCtrl =
        TabController(length: controller.state.dataSource.length, vsync: this)
          ..addListener(() {
            if (controller.state.tabCtrl.indexIsChanging) {
              if (controller.state.tabCtrl.index !=
                  controller.state.tabCtrl.previousIndex) {
                controller.state.currentIndex.value =
                    controller.state.tabCtrl.index;
              }
            }
          });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: detailAppBar(
        context,
        actions: [
          InkWell(
            onTap: controller.actionSearch,
            child: Padding(
                padding: EdgeInsets.only(left: 20.r),
                child: Center(
                    child: Icon(
                  IconUtil.search,
                  size: 15.r,
                  color: ColorsUtil.title,
                ))),
          ),
          InkWell(
            onTap: controller.actionManage,
            child: Padding(
                padding: EdgeInsets.symmetric(horizontal: gap12),
                child: Center(
                  child: Text(
                    '管理',
                    style: pfMediumStyle(
                        color: ColorsUtil.green, fontSize: font14),
                  ),
                )),
          )
        ],
      ),
      body: CustomScrollView(
        slivers: [
          SliverToBoxAdapter(
            child: HeaderMoreView(),
          ),
          SliverPersistentHeader(
              floating: true, pinned: true, delegate: AppMoreHeaderDelegate()),
          sliverTableView(data: controller.state.dataSource)
        ],
      ),
    );
  }
}

class AppMoreHeaderDelegate extends SliverPersistentHeaderDelegate {
  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return HeaderMenuView();
  }

  @override
  double get maxExtent => 50.r;

  @override
  double get minExtent => 50.r;

  @override
  bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) {
    return true;
  }
}
